package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.Result;
import java.util.List;
import java.util.Map;

// 收货地址
@RestController
public class AddressController {
    @Autowired
    private AddressService addressService;

    // 地址列表
    @GetMapping("/address/list")
    public ResultInfo findList(){
        // 1.调用service
        List<Address> addressList = addressService.findList();
        // 2.返回结果
        return ResultInfo.success(addressList);
    }


    // 新增地址
    @PostMapping("/address")
    public ResultInfo save(@RequestBody Address address) { // 1.接收请求参数
        // 2.调用service新增
        addressService.save(address);
        // 3.返回结果
        return ResultInfo.success();
    }

    // 设为默认地址
    @PutMapping("/address/default")
    public ResultInfo setDefault(@RequestBody Map<String,Long> param){
        // 1.接收请求参数
        Long id = param.get("id");
        // 2.调用serivce设置
        addressService.setDefault(id);
        // 3.返回结果
        return ResultInfo.success();
    }


    // 去结算（查询默认收货地址）
    @GetMapping("/address/default")
    public ResultInfo findDefault(){
        //1.调用service查询
        Address address = addressService.findDefault();
        //2.返回结果
        return ResultInfo.success(address);
    }


    // 回显地址
    @GetMapping("/address/{id}")
    public ResultInfo get(@PathVariable Long id) {// 1.接收路径参数
        // 2.调用service查询
        Address address = addressService.findById(id);
        if (address != null){
            return ResultInfo.success(address);
        }else {
            return ResultInfo.error("没有找到该对象");
        }
    }

    //修改地址
    @PutMapping("/address")
    public ResultInfo update(@RequestBody Address address){
        addressService.update(address);
        return ResultInfo.success(null);
    }


    //删除地址
    @DeleteMapping("/address")
    public ResultInfo delete(Long ids){
        addressService.delete(ids);
        return ResultInfo.success(null);
    }
}
